{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "通过最简单的一个神经网络来探索一般性建模方法和思路"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "import torch.nn as nn\n",
    "import torch.optim as optim"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 定义一个最简单的单层神经网络\n",
    "class SimpleNN(nn.Module):\n",
    "    def __init__(self, input_size, output_size):\n",
    "        super(SimpleNN, self).__init__()\n",
    "        self.fc = nn.Linear(input_size, output_size)\n",
    "\n",
    "    def forward(self, x):\n",
    "        return self.fc(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "OrderedDict([('fc.weight', tensor([[ 0.1923, -0.0719,  0.0813, -0.2961, -0.1164,  0.1152, -0.1251,  0.3233,\n",
      "         -0.2226,  0.0083],\n",
      "        [-0.2511,  0.0863, -0.2746,  0.1593,  0.2541,  0.0477, -0.2439,  0.1366,\n",
      "         -0.2304, -0.2611]])), ('fc.bias', tensor([-0.0184, -0.2601]))])\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/opt/anaconda3/envs/science39/lib/python3.9/site-packages/torch/autograd/__init__.py:266: UserWarning: CUDA initialization: The NVIDIA driver on your system is too old (found version 11080). Please update your GPU driver by downloading and installing a new version from the URL: http://www.nvidia.com/Download/index.aspx Alternatively, go to: https://pytorch.org to install a PyTorch version that has been compiled with your version of the CUDA driver. (Triggered internally at ../c10/cuda/CUDAFunctions.cpp:108.)\n",
      "  Variable._execution_engine.run_backward(  # Calls into the C++ engine to run the backward pass\n"
     ]
    }
   ],
   "source": [
    "# 创建一个简单的神经网络实例\n",
    "input_size = 10\n",
    "output_size = 2\n",
    "model = SimpleNN(input_size, output_size)\n",
    "\n",
    "# 定义损失函数和优化器\n",
    "criterion = nn.MSELoss()\n",
    "optimizer = optim.SGD(model.parameters(), lr=0.01)\n",
    "\n",
    "# 输入数据\n",
    "input_data = torch.randn(1, input_size)\n",
    "\n",
    "# 正向传播和反向传播\n",
    "output = model(input_data)\n",
    "target = torch.randn(1, output_size)\n",
    "loss = criterion(output, target)\n",
    "optimizer.zero_grad()\n",
    "loss.backward()\n",
    "optimizer.step()\n",
    "\n",
    "# 输出训练后的模型参数\n",
    "print(model.state_dict())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-2.51275771 12.11610903]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "class SimpleNN:\n",
    "    def __init__(self, input_size, output_size):\n",
    "        # 初始化权重和偏置\n",
    "        self.W = np.random.randn(output_size, input_size)  # 权重矩阵\n",
    "        self.b = np.random.randn(output_size)             # 偏置向量\n",
    "\n",
    "    def forward(self, x):\n",
    "        # 实现线性变换 y = W * x + b\n",
    "        return np.dot(self.W, x) + self.b\n",
    "\n",
    "# 测试\n",
    "model = SimpleNN(input_size=3, output_size=2)\n",
    "x = np.array([1.0, 2.0, 3.0])  # 输入\n",
    "output = model.forward(x)\n",
    "print(output)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "science39",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
